public class Test_One {
    public static void main(String[] args) {
        Dog d = new Dog("小哈",2,"黑色");
        Person p1 = new Person("王",30);
        p1.Feeding_Pets(d,"肉骨头");
        Cat c = new Cat("小花",3,"灰色");
        Person p2 = new Person("李",25);
        p2.Feeding_Pets(c,"鱼罐头");
    }
}

//抽象动物类
abstract class Animal {
    String name;
    int age;
    String color;
    void eat(String something) {
        System.out.println(age+"岁"+"的"+color+"的"+name+"喜欢吃"+something);
    }
}

class Dog extends Animal {
    public Dog() {
    }
    public Dog(String name,int age,String color){
        this.name = name;
        this.age =age;
        this.color = color;
    }
    void LookHome(){
        System.out.println(name+"忠实地看家");
    }
}

class Cat extends Animal {
    public Cat(String name,int age,String color){
        this.name = name;
        this.age =age;
        this.color = color;
    }
    void catchMouse(){
        System.out.println(name+"奋力地抓老鼠");
    }
}
class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
//自动向上转型
    void Feeding_Pets(Animal animal, String something){
        System.out.println(age+"岁"+"的"+name+"养了一只"+animal.color+"的"+animal.age+"岁"+"的"+animal.name);
        animal.eat(something);
        if(animal instanceof Dog) {
            Dog d = (Dog) animal;
            d.LookHome();
        }
        if(animal instanceof Cat) {
            Cat c = (Cat) animal;
            c.catchMouse();
        }
    }
}
//instanceof左边显式声明的类型与右边操作元必须是同种类或存在继承关系，
// 也就是说需要位于同一个继承树，否则会编译错误
